package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.TaskPlan;
import com.hzhc.mapper.TaskPlanMapper;
import com.hzhc.service.TaskPlanService;
import com.hzhc.utils.CronUtils;
import com.hzhc.vo.TaskPlanVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 任务管理 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-04-07
 */
@Service
@RequiredArgsConstructor
public class TaskPlanServiceImpl extends ServiceImpl<TaskPlanMapper, TaskPlan> implements TaskPlanService {
    @Override
    public List<TaskPlanVo> getTaskPlanVoList(Page<TaskPlanVo> page, TaskPlanVo vo) {
        IPage<TaskPlanVo> iPage = baseMapper.selectTaskPlanVoList(page,vo);
        return iPage.getRecords();
    }

    @Override
    public Boolean checkTaskPlanByField(String field, String fieldStr, Long id) {
        QueryWrapper<TaskPlan> queryWrapper = new QueryWrapper<TaskPlan>();
        queryWrapper.eq(field,fieldStr);
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        List<TaskPlan> taskPlanList = baseMapper.selectList(queryWrapper);
        return !taskPlanList.isEmpty();
    }

    @Override
    public Boolean addTaskPlan(TaskPlan taskPlan) {
        boolean bool = save(taskPlan);
        return bool;
    }

    @Override
    public Boolean updateTaskPlan(TaskPlan taskPlan) {
        boolean bool = updateById(taskPlan);
        return bool;
    }

    @Override
    public Boolean delTaskPlan(Integer id) {
        boolean bool = removeById(id);
        return bool;
    }

    @Override
    public Boolean updateTaskPlanStatus(TaskPlan taskPlan) {
        TaskPlan plan = getById(taskPlan.getId());
        plan.setStatus(taskPlan.getStatus());
        boolean bool = updateById(plan);
        return bool;
    }

    @Override
    public Boolean checkCronExpressionIsValid(String cronExpression) {
        boolean bool = CronUtils.isValid(cronExpression);
        return bool;
    }
}
